import requests
import time
from get_traceid import get_traceid

# 初始Cookie字典
cookies = {
    'lastCity': '101250100',
    'ab_guid': 'f267f038-4d71-4141-9027-30d8e4786076',
    '__zp_seo_uuid__': 'eab3a0a3-0fd4-440d-bffd-ff5ec8b54162',
    '__l': 'r=https%3A%2F%2Fcn.bing.com%2F&l=%2F&s=1',
    '__g': '-',
    'Hm_lvt_194df3105ad7148dcf2b98a91b5e727a': '1751033224',
    'HMACCOUNT': '0B95619A2CE0108E',
    'wt2': 'DX5PTbUxpBkaRCgQhD_2hRL94ackyk2hfHLhlI9CBmIjQfAvpM1_eDMg_Q9rIQGKau-AtVyqCHF5S1Er6kS6j1w~~',
    'wbg': '0',
    'zp_at': 'GGpNBg_zBwUKeKgwMr5NbPyEesI-fMCKznKntfZHA3k~',
    'Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a': '1751034647',
    'bst': 'V2R9wmFu362lhjVtRuyhwfLSmw7Drexi8~|R9wmFu362lhjVtRuyhwfLSmw7DrTwy4~',
    '__c': '1751033224',
    '__a': '91880635.1715929128.1721463024.1751033224.273.3.5.52',
    '__zp_stoken__': '8ed5fwr7DsWNEHmlsHGYZf3vCgXBucMODwoVXwoZra8OCwq9pccKgw4LCtXBpVcKpTcKnw47CknHDgsKfwplxwrXCs8KUwrXCu03CmcK7w7DDicSFw4jColzCtcKhwpXCocO7wpbDv8Kyw7nDi8Otw4HDr8KvxIfCoMO6wrvDtsSCw5jCnsOhw4LFhsK9xLXCt8Oew4PFgMSLxKbDicS5wqPEq8KzxI%2FCt8OXxa3EqsSCxZnErMO8w4DCoT85ExQUEBYUExMPERoVEBQSHhkZFRsYFxcbFUY8xIDDs8KiREZHRDBaVVUWXGdrUGdQDWFNU0JDEBQcGUM6QERCP8OFw4DDgC%2FDisOAw4RMw4fCv8OEGERKP0DDgzkzSWIUdhZfFE0QbxPDkGhmwpXEh8ODajhEQcOAxL1LQR1KQz9HRENIP0EtQzjDlGpdwpfEiMK%2Fwr00QCNHQUROQT9BRExDSS1ESDoxQT8wRw0aFRcTMkjDg8Kdw4DDpUFE',
}

def get_joblist(timestamp):
    headers = {
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'priority': 'u=1, i',
        'referer': 'https://www.zhipin.com/web/geek/jobs?city=101010100&query=go',
        'sec-ch-ua': '"Microsoft Edge";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'token': 'thW1RhMUNhpphMS8',
        'traceid': get_traceid(timestamp),  # 使用动态生成的traceid
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0',
        'x-requested-with': 'XMLHttpRequest',
        'zp_token': 'V2R9wmFu362lhjVtRuyhwfLSmw7Drexi8~|R9wmFu362lhjVtRuyhwfLSmw7DrTwy4~',
    }

    params = {
        'page': '1',
        'pageSize': '15',
        'city': '101010100',
        'query': 'go',
        'expectInfo': '',
        'multiSubway': '',
        'multiBusinessDistrict': '',
        'position': '',
        'jobType': '',
        'salary': '',
        'experience': '',
        'degree': '',
        'industry': '',
        'scale': '',
        'stage': '',
        'scene': '1',
        '_': str(timestamp),  # 动态时间戳
    }

    response = requests.get(
        'https://www.zhipin.com/wapi/zpgeek/search/joblist.json',
        params=params,
        cookies=cookies,
        headers=headers
    )
    
    # 更新Cookie（处理Set-Cookie响应）
    if response.headers.get('Set-Cookie'):
        # 解析Set-Cookie并更新到cookies字典
        print(response.headers.get('Set-Cookie'))
        set_cookie_headers = response.headers.get('Set-Cookie').split(', ')
        for cookie in set_cookie_headers:
            # 分割cookie键值对（忽略过期时间等参数）
            cookie_parts = cookie.split(';')[0].split('=')
            if len(cookie_parts) == 2:
                key, value = cookie_parts
                cookies[key] = value
    
    return response.json()

# 执行三次请求并打印结果
for i in range(3):
    print(f"===== 第 {i+1} 次请求 =====")
    timestamp = int(time.time() * 1000)  # 每次请求使用新的时间戳
    result = get_joblist(timestamp)
    print(result)
    print("\n")
    time.sleep(2)  # 间隔2秒，避免请求过于频繁